#!/usr/bin/env python3

menu = {
  "家电" : {
      "电视" : [
          ["乐视","3000"],
          ["小米","2200"]
      ],
      "空调" : [
          ["奥克斯","5000"],
          ["格力","6000"]
      ],
      "洗衣机" : [
          ["LG","4000"],
          ["西门子","2000"]
      ],
  },
  "母婴" : {
      "奶粉" : [
          ["美素","400"],
          ["雀巢","300"]
      ],
      "玩具" : [
          ["积木","500"],
          ["小汽车","50"]
      ],
  },
  "图书" : {
      "少儿" : [
          ["绘本","30"],
          ["折纸书","50"]
      ],
      "科技" : [
          ["人人都是产品经理","100"],
          ["Python自动化教程","200"]
      ],
  },
}   # 通过字典实现三级菜单


shopping_list = []


# 循环打印menu一级分类目录
while True:
    print("商品分类".center(20,"-"))    # 打印商品目录
    for key in menu:    # 打印菜单
        print(key)
    choice = input("请选择您想购买的商品类别，退出请输入[q]>>:")  # 输入一个变量
    if choice not in menu: continue  # 如果 choice 变量没在 menu 字典菜单内，则退出本次循环
    if choice == "q":exit("退出购买")   # 如果 choice = q,则退出整个程序


    # 循环打印menu二级分类目录
    while True:
        print("商品分类".center(20, "-"))  # 打印商品目录
        for key2 in menu[choice]:
            print(key2)
        choice2 = input("请选择您想购买的商品类别，返回上级输入[b],退出输入[q]>>:")
        if choice2 not in menu[choice]: continue
        if choice2 == "b":break # 如果 choice2 = b ，则退出本循环
        if choice2 == "q": exit("退出购买")

        # 循环打印menu三级商品目录
        while True:
            print("商品分类".center(20, "-"))  # 打印商品目录
            for index,key3 in enumerate(menu[choice][choice2]): # 使用enumerate 创建索引序号并打印
                print(index,key3[0],key3[1])
            choice3 = input("请选择您想购买的商品序号，返回上级输入[b],退出输入[q],查看购物车[v]>>:")
            if choice3 == "b":break
            if choice3 == "v":continue
            if choice3 == "q":exit("退出购买")
            if choice3.isdigit():   # 如果 choice3 是数字，则变更类型为int型
                choice3 = int(choice3)
            if choice3 >= 0 and choice3 < len(menu[choice][choice2]):   # 如果 choice3>=0并且< menu列表数，则跳出本次循环
                p = menu[choice][choice2][choice3]
                shopping_list.append(p)
                choice3 = str(choice3)
            else:   # 否则打印，您选的商品不存在
                print("你选的商品不存在，请从新选择")

        while True:
            total = 0
            print("您的购物车".center(30,"-"))
            for i,v in enumerate(shopping_list):    # 打印购物车列表，并计算总消费金额
                print(i,v[0],v[1])
                total += int(v[1])
            print("结算".center(30,"-"))
            print("总金额=%s" %total)
            choice4 = input("返回上级输入[b],退出输入[q],返回购物车[v],结算请输入金额>>:")
            if choice4 == "b": break
            if choice4 == "v": continue
            if choice4 == "q": exit("退出购买")
            if choice4.isdigit():  # 如果 choice4 是数字，则变更类型为int型
                choice4 = int(choice4)
            if choice4 < total: # 判断支付金额是否充足
                print("您的支付金额不足")
                continue
            else:
                print(choice4,"-",total,"=","余额:",choice4-total )
                choice5 = input("请确认您的支付金额，确认是否购买y/n>>:")
                if choice5 == "y":exit("完成支付，购买成功")
                if choice5 == "n":break










